package algorithm_demo.leetcode;

/**
 * 121. 买卖股票的最佳时机
 * <a href="https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/">https://leetcode.cn/problems/best-time-to-buy-and-sell-stock/</a>
 * @author Api
 * @date 2023/5/9 0:34
 */
public class Problem_121_BestTimeToBuyAndSellStock {
    public int maxProfit(int[] prices) {
        int i = 0;//记录最小价格下标
        int max= 0; //记录差价
        int j = 0;//用来进行遍历
        while(j<prices.length){
            if(prices[i]>prices[j]){
                i = j;
            }else if(prices[j] - prices[i] > max){
                max = prices[j] - prices[i];
            }
            j++;
        }
        return max;
    }
}
